use std::iter;

use crate::{ui::UiOutput};

use super::{
    pass::ui_pass::{ScreenDescriptor, UIPass},
    rhi::Rhi,
    ScreenDescriptorInfo, RenderPipelineInitInfo,
};

pub struct UiRenderPipeline {
    // main_camera_pass: MainCameraPass,
    gui_pass: UIPass,
    rhi: Rhi,
}

impl UiRenderPipeline {
    pub fn draw(&mut self, view: &wgpu::TextureView, ui_output: &UiOutput, screen_desc: &ScreenDescriptorInfo) {

        {
            let mut encoder =
                self.rhi
                    .borrow()
                    .device
                    .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                        label: Some("Ui Render Encoder"),
                    });

            let screen_descriptor = ScreenDescriptor {
                physical_width: screen_desc.view_port_size.x,
                physical_height: screen_desc.view_port_size.y,
                scale_factor: screen_desc.scale_factor,
            };

            {
                self.gui_pass
                    .add_textures(
                        &self.rhi.borrow().device,
                        &self.rhi.borrow().queue,
                        &ui_output.texture_delta,
                    )
                    .expect("add texture ok");
                self.gui_pass.update_buffers(
                    &self.rhi.borrow().device,
                    &self.rhi.borrow().queue,
                    &ui_output.primitive,
                    &screen_descriptor,
                );

                // Record all render passes.

                self.gui_pass
                    .draw(
                        &mut encoder,
                        &view,
                        &ui_output.primitive,
                        &screen_descriptor,
                        Some(wgpu::Color::TRANSPARENT),
                    )
                    .unwrap();
                self.gui_pass
                    .remove_textures(&ui_output.texture_delta)
                    .expect("remove texture ok");
            }
            self.rhi.borrow().queue.submit(iter::once(encoder.finish()));
        }
       
    }
}

impl UiRenderPipeline {
    pub fn new(init_info:RenderPipelineInitInfo) -> Self {
        let gui_pass = UIPass::new(init_info.rhi.clone(), 1);
    
        Self { gui_pass, rhi:init_info.rhi }
    }
    // // pub fn draw(&self , render_scene:&RenderScene)
}
